package inbound.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import lombok.Data;
import model.HttpConfig;

/**
 * @author dsp
 * @date 2021-04-04
 */
@Data
public class HibInit extends ChannelInitializer<SocketChannel> {

    private HttpConfig httpConfig;
    private int maxContentLength;

    public HibInit(HttpConfig httpConfig) {
        this.httpConfig = httpConfig;
        this.maxContentLength = httpConfig.getMaxContentLength();
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline p = socketChannel.pipeline();
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(maxContentLength * maxContentLength));
        p.addLast("contentDecompressor", new HttpContentDecompressor());
        p.addLast(new HibHandler(httpConfig));
    }
}
